package com.tanhua.management.service;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.management.mapper.LogMapper;
import com.tanhua.management.pojo.Log;
import com.tanhua.management.vo.LogsVo;
import com.tanhua.management.vo.PageResult;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author why
 */
@Service
public class ManageLogService {

    @Autowired
    private LogMapper logMapper;



    public PageResult getLogs(Integer page, Integer pageSize, String sortProp, String sortOrder, String type, Long uid) {

        PageResult pageResult=new PageResult();


//
        QueryWrapper<Log> wrapper = new QueryWrapper<>();
        wrapper.eq("type",type);
        wrapper.eq("user_id",uid);
        wrapper.orderBy(true, StringUtils.equals("ascending",sortOrder),"log_time");
        IPage<Log> logIPage=new Page<>(page-1,pageSize);
        logIPage = this.logMapper.selectPage(logIPage, wrapper);


        //PageInfo<Log> logPageInfo = this.logApi.getLog(uid, page, pageSize, sortProp, sortOrder ,type);

        if (CollectionUtils.isEmpty(logIPage.getRecords())){
            return null;
        }
        List<LogsVo> logsVoList =new ArrayList<>();

        List<Log> records = logIPage.getRecords();

        for (Log record : records) {
            LogsVo logsVo=new LogsVo();
            logsVo.setId(record.getId().intValue());
            logsVo.setUserId(uid.intValue());
            logsVo.setEquipment(record.getEquipment());
            logsVo.setLogTime(record.getLogTime().intValue());
            logsVo.setPlace(record.getPlace());

            logsVoList.add(logsVo);
        }

        pageResult.setPage(page);

        pageResult.setPageSize(pageSize);

        int pages = Integer.parseInt(String.valueOf(logIPage.getPages()));
        pageResult.setPages(pages);
        pageResult.setCounts(Integer.parseInt(String.valueOf(logIPage.getTotal())));
        pageResult.setItems(logsVoList);
        return pageResult;
    }

}
